package org.cttic.visualization.visualization.z_service.kafka_lw;

import com.alibaba.fastjson.JSONObject;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.model.Filters;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.bson.BsonDocument;
import org.bson.Document;
import org.cttic.visualization.visualization.common.commonConf;
import org.cttic.visualization.visualization.common.mongoData;
import org.cttic.visualization.visualization.util.ParseJsonDataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.kafka.annotation.KafkaHandler;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.List;

/**
 * @author shkstart
 * @create 2020-05-14 9:00
 */
@Component
@Slf4j
public class lwzx_information {
    @Autowired
    private MongoTemplate mongoTemplate;
    @KafkaHandler
//    @KafkaListener(topics = commonConf.lwzx_kafkaTopic)
    public void registryReceiver(List<String> record) throws ParseException {
//        System.out.println("record:"+record);
        if(record!=null&&record.size()>0){
        for (String rec : record) {
        JSONObject jsonData = ParseJsonDataUtil.getJsonData(rec);
            String vehiclePlate = jsonData.getString("vehiclePlate");

        log.debug("lwzx_kafkaTopic接收车牌：",vehiclePlate);
        FindIterable<Document> documents = mongoTemplate.getDb().getCollection(commonConf.mongo)
                .find(Filters.eq("vehiclePlate", vehiclePlate));
            MongoCursor<Document> iterator1 = documents.iterator();
                //未发现，插入数据
                if(!iterator1.hasNext()){
                    System.out.println("***************向mongo插入数据******************");
                    Document document = new Document("vehiclePlate", vehiclePlate).append("vehicleColor", jsonData.getIntValue("vehicleColor"))
                            .append("tansTime", jsonData.getString("tansTime"))
                            .append("vehicleType", jsonData.getIntValue("vehicleType"))
                            .append("vehicleClass", jsonData.getIntValue("vehicleClass"))
                            .append("axleCount", jsonData.getIntValue("axleCount"))
                            .append("mediaType",jsonData.getIntValue("mediaType"));
                    mongoTemplate.getDb().getCollection(commonConf.mongo).insertOne(document);
                    System.out.println("***************mongo插入数据完成******************");
                    //发现数据
                }else {
                    MongoCursor<Document> iterator = documents.iterator();
                    while (iterator.hasNext()){
                        String data = iterator.next().toJson();
                        JSONObject json2 = ParseJsonDataUtil.getJsonData(data);
//                        String mediaType = jsonData.getString("mediaType");
                        int mediaType = jsonData.getIntValue("mediaType");
                        if(mediaType!=json2.getIntValue("mediaType")){
                            //更新数据库
                            System.out.println("***************向mongo更新数据******************");
                            mongoTemplate.getDb().getCollection(commonConf.mongo)
                                    .findOneAndUpdate(new Document("vehiclePlate",vehiclePlate),new Document("mediaType",mediaType));
                            System.out.println("***************mongo更新数据完成******************");
                        }
                    }
                }
            }
        }

    }
}